import _ from 'lodash';
const TITLE_FMT = (o) => {
  var onclick = '';
  if (o.id && o.entityName) {
    var url = process.env.API_URL + "/datav/view/viewContent.jsp?entityName=" + o.entityName + "&id=" + o.id;

    onclick = "onclick='openFrame(\"" + url + "\")'";
  }
  return `<div class="item title" ${onclick}>${o.value}</div>`;
};
const IMG_FMT = (o) => {
  return `<div class="item img">
    <img src="${process.env.PRODUCT_URL}${o.value}">
  </div>`;
};
const DESC_FMT = (o) => {
  return `<p class="item desc">${o.value}</p>`;
};
const KVS_FMT = {
  'title': TITLE_FMT,
  'img': IMG_FMT,
  '描述': DESC_FMT
};
const ICONS = {
  '门店地址': '<i class="iconfont icon-dizhi"></i>',
  '地址': '<i class="iconfont icon-dizhi"></i>',
  '联系人': '<i class="iconfont icon-lianxiren"></i>',
  '联系方式': '<i class="iconfont icon-dianhua"></i>'
};
export default {
  _,
  calcPercent: function (arr, fix = 0, item = 'value', result = 'percent') {
    // 计算总额
    var summary = _.sumBy(arr, item);
    arr.forEach(o => {
      o[result] = (o[item] / summary * 100).toFixed(0) + '%';
    });
  },
  refineMonth: function (data, reverse) {
    if (reverse) {
      data.reverse();
    }
    data.forEach((n) => {
      var month = parseInt(n.name.split('.')[1]);
      n.month = month;
      n.year = parseInt(n.name.split('.')[0]);
      n.name = month + '月';
    });
  },
  obj2HtmlDom: function (data, kvs = KVS_FMT) {
    var html = '<div class="form-util">';
    data.forEach((o) => {
      var fmt = kvs[o.name];
      if (fmt) {
        html += fmt(o);
      } else {
        var iconKey = ICONS[o.name];
        if (iconKey) {
          html += `
            <div class="item">
              <div class="value">${iconKey} ${o.value}</div>
            </div>
          `;
        } else {
          html += `
          <div class="item flex">
            <div class="key">${o.name}：</div>
            <div class="value">${o.value}</div>
          </div>
        `;
        }
      }      
    }); 
    html += '</div>';
    return html;
  },
  TITLE_FMT,
  IMG_FMT,
  KVS_FMT
};
